﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SharePointDatabaseExplorer
{
    public partial class frmDatabaseConnection : Form
    {
        private string connString = string.Empty;
        public frmDatabaseConnection()
        {
            InitializeComponent();
        }

        private void frmDatabaseConnection_Load(object sender, EventArgs e)
        {
            txtSQLServer.Text = Database._SQLSever;
            txtDatabase.Text = Database._DatabaseName;
            txtUserID.Text = Database._UserID;
            txtPassword.Text = Database._Password;
            ckIntegratedAuth.Checked = Database._IntegratedSecurity;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (TestConnection())
                {
                    Database objDatabase = new Database();
                    objDatabase.SQLServer = txtSQLServer.Text.Trim();
                    objDatabase.DatabaseName = txtDatabase.Text.Trim();
                    objDatabase.UserID = txtUserID.Text.Trim();
                    objDatabase.Password = txtPassword.Text.Trim();
                    objDatabase.IntegratedSecurity = ckIntegratedAuth.Checked;
                    objDatabase.ConnectionString = connString;
                    objDatabase.FileOutputPath = txtOutputFolder.Text;
                    this.Close();
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show("The following error occurred: " + exp.Message, "Error Occurred", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.Close();
            }
        }

        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            if (TestConnection())
            {
                MessageBox.Show("Connection Successful", "Connection Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private bool TestConnection()
        {
            //SqlConnection myConnection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;Initial Catalog=test;Integrated Security=true;");
            string sSecurity = string.Empty;
            if (ckIntegratedAuth.Checked)
            {
                sSecurity = "Integrated Security=true";
            }
            else
            {
                sSecurity = "User ID=" + txtUserID.Text.Trim() + ";Password=" + txtPassword.Text.Trim();
            }
            connString = "Data Source=" + txtSQLServer.Text.Trim() + ";Initial Catalog=" + txtDatabase.Text.Trim() + ";" + sSecurity;
            SqlConnection myConnection = new SqlConnection(connString);
            
            try
            {
                this.Cursor = Cursors.WaitCursor;
                myConnection.Open();
                this.Cursor = Cursors.Default;
                return true;
            }
            catch (Exception exp)
            {
                this.Cursor = Cursors.Default;
                MessageBox.Show(exp.Message);
                return false;
            }
        }

        private void ckIntegratedAuth_CheckedChanged(object sender, EventArgs e)
        {
            Boolean bEnabled = true;

            if (ckIntegratedAuth.Checked)
            {
                bEnabled = false;
            }

            txtUserID.Enabled = bEnabled;
            txtPassword.Enabled = bEnabled;
        }

        private void btnSelectFolder_Click(object sender, EventArgs e)
        {
            if (SelectFolder.ShowDialog() == DialogResult.OK)
            {
                txtOutputFolder.Text = SelectFolder.SelectedPath;
            }
        }
    }
}
